package com.wufun.overseas.adapters;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.wufun.baselibrary.utils.LogUtils;
import com.wufun.overseas.R;
import com.wufun.overseas.common.SettingTypeInterface;
import com.wufun.overseas.models.bean.SettingModel;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by tangbl on 2017/12/4 0004.
 *
 */

public class SettingAdapter extends BaseAdapter {

    private Context context;
    private LayoutInflater layoutInflater;
    private ViewHolder holder;
    private boolean isShow = false;

    private List<SettingModel> dataList = new ArrayList<>();
    private String[] titles;
//    private int[] imgs = {R.mipmap.ic_transfer, R.mipmap.ic_gamepad, R.mipmap.ic_downloadmanager
//            , R.mipmap.ic_feedback, R.mipmap.ic_more};
//    private int[] types = {SettingTypeInterface.TYPE_TRANSFER
//            , SettingTypeInterface.TYPE_GAMEPAD, SettingTypeInterface.TYPE_DOWNLOAD
//            , SettingTypeInterface.TYPE_FEEDBACK, SettingTypeInterface.TYPE_MORE};

    private int[] imgs = {R.mipmap.ic_gamepad, R.mipmap.ic_downloadmanager
            , R.mipmap.ic_feedback, R.mipmap.ic_more};
    private int[] types = {SettingTypeInterface.TYPE_GAMEPAD, SettingTypeInterface.TYPE_DOWNLOAD
            , SettingTypeInterface.TYPE_FEEDBACK, SettingTypeInterface.TYPE_MORE};

    public SettingAdapter(Context context) {
        this.context = context;
        layoutInflater = LayoutInflater.from(context);

        titles = context.getResources().getStringArray(R.array.setting_titles);
        for (int i=0;i<titles.length;i++) {
            SettingModel temp = new SettingModel(imgs[i], titles[i], false, types[i]);
            dataList.add(temp);
        }
        String appFlag = context.getResources().getString(R.string.app_flag);
        if (appFlag.equals("APP_DC") || appFlag.equals("APP_N64")) {
            for (int i=0;i<types.length;i++) {
                if (types[i] == SettingTypeInterface.TYPE_GAMEPAD) {
                    dataList.remove(i);
                    break;
                }
            }
        }
        LogUtils.e(">>>>>:"+dataList.size());
    }

    @Override
    public int getCount() {
        return dataList.size();
    }

    @Override
    public SettingModel getItem(int position) {
        return dataList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = layoutInflater.inflate(R.layout.item_setting_view, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        SettingModel temp = dataList.get(position);
        holder.settingIv.setImageResource(temp.getImg());
        holder.settingTv.setText(temp.getTitle());
        if (temp.getType()==SettingTypeInterface.TYPE_DOWNLOAD && isShow)
            holder.settingTipIv.setVisibility(View.VISIBLE);
        else
            holder.settingTipIv.setVisibility(View.INVISIBLE);

        return convertView;
    }

    public void setShow(boolean show) {
        isShow = show;
        notifyDataSetChanged();
    }

    static class ViewHolder {
        @Bind(R.id.setting_iv)
        ImageView settingIv;
        @Bind(R.id.setting_tip_iv)
        ImageView settingTipIv;
        @Bind(R.id.setting_tv)
        TextView settingTv;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
